package com.gxuwz.controller;

import com.gxuwz.entity.Department;
import com.gxuwz.entity.Instructor;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.Code;
import com.gxuwz.result.PageInfo;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.ClassesService;
import com.gxuwz.service.DepartmentService;
import com.gxuwz.service.InstructorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description:辅导员信息管理模块
 */

@Controller
@RequestMapping("/instructor")
public class InstructorController {
    @Autowired
    private InstructorService instructorService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * @Description:查询辅导员信息，并跳转至辅导员信息管理页面
     * @Param:  [com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Instructor]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/list")
    public ModelAndView instructorList(PageInfo pageInfo, ModelAndView modelAndView, Instructor instructor){
        System.out.println(pageInfo);
        PageVo pageVo = instructorService.getInstructorList(instructor, pageInfo);
        modelAndView.setViewName("instructorManage/instructorManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现添加辅导员信息功能，并跳转至辅导员信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Instructor]
     * @Return: org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping("/addInstructor")
    public ModelAndView addInstructor(ModelAndView modelAndView,Instructor instructor){
        boolean flag = instructorService.addInstructorInfo(instructor);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_OK,"新增成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_ERR,"新增失败"));
        }
        PageVo pageVo = instructorService.getInstructorList(null, pageInfo);
        modelAndView.setViewName("instructorManage/instructorManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }
    /**
     * @Description:跳转至添加辅导员信息页面
     * @Param:  [org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/addInstructorPage")
    public ModelAndView queryIds(ModelAndView modelAndView){
        List<String> ids = instructorService.selectInstructorIds();
        List<Department> depList = departmentService.getDepartmentIDAndName();
        modelAndView.addObject("depList",depList);
        modelAndView.addObject("instructorIds",ids);
        modelAndView.setViewName("instructorManage/addInstructor");
        return modelAndView;
    }

    /**
     * @Description:实现单独删除辅导员信息功能，并跳转至辅导员信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteInstructorOne")
    public ModelAndView deleteInstructorOne(String instId, ModelAndView modelAndView){
        boolean flagClasses = classesService.selectClassesExistByInstID(instId);
        PageInfo pageInfo = new PageInfo();
        if(flagClasses){
            boolean flag = instructorService.deleteInstructorOne(instId);
            if(flag){
                modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"删除成功"));
            } else {
                modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败"));
            }
            PageVo pageVo = instructorService.getInstructorList(null, pageInfo);
            modelAndView.setViewName("instructorManage/instructorManage");
            modelAndView.addObject("pageVo",pageVo);
            return modelAndView;
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败,请先处理该辅导员管理的班级"));
            PageVo pageVo = instructorService.getInstructorList(null, pageInfo);
            modelAndView.setViewName("instructorManage/instructorManage");
            modelAndView.addObject("pageVo",pageVo);
            return modelAndView;
        }

    }

    /**
     * @Description:实现跳转至修改辅导员信息页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/updateInstructorPage")
    public ModelAndView updateInstructorPage(String instId,ModelAndView modelAndView){
        System.out.println(instId);
        Instructor instructorOne = instructorService.getInstructorOne(instId);
        System.out.println(instructorOne);
        List<String> ids = instructorService.selectInstructorIds();
        List<Department> depList = departmentService.getDepartmentIDAndName();
        modelAndView.addObject("depList",depList);
        modelAndView.addObject("instIds",ids);
        modelAndView.addObject("instructorOne",instructorOne);
        modelAndView.setViewName("instructorManage/editInstructor");
        return modelAndView;
    }

    /**
     * @Description:实现修改辅导员信息功能，并跳转至辅导员信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Instructor, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateInstructor")
    public ModelAndView updateInstructor(ModelAndView modelAndView,Instructor instructor,String oldId){
        System.out.println(oldId);
        boolean flag = instructorService.updateInstructorInfo(instructor,oldId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改失败"));
        }
        PageVo pageVo = instructorService.getInstructorList(null, pageInfo);
        modelAndView.setViewName("instructorManage/instructorManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }
}


